package testutil;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.ejb.Stateful;

import dao.QuestionDAORemote;
import dao.TemplateCategoryDAORemote;
import dto.CandidateDTO;
import dto.TemplateCategoryDTO;
import dto.TemplateDTO;
import dto.QuestionDTO;

/**
 * Session Bean implementation class TestGenerateBean
 */
@Stateful
public class TestGenerateBean implements TestGenerateBeanRemote {
	
	@EJB
	TemplateCategoryDAORemote tcDAO;
	@EJB
	QuestionDAORemote qDAO;
	
	TemplateDTO template;
	CandidateDTO candidate;
	List<QuestionDTO> questions;

	@PostConstruct
	public void init() {
    	this.questions = new ArrayList<QuestionDTO>();
	}
	
    public TestGenerateBean() {
    }

	public TemplateDTO getTemplate() {
		return template;
	}

	public void setTemplate(TemplateDTO template) {
		System.out.println("Template was set!");
		this.template = template;
	}

	public CandidateDTO getCandidate() {
		return candidate;
	}

	public void setCandidate(CandidateDTO candidate) {
		System.out.println("Candidate was set!");
		this.candidate = candidate;
	}

	public List<QuestionDTO> getQuestions() {
		System.out.println("Returning " + questions.size() + " questions!");
		return questions;
	}

	public void generateQuestions() {
		for (TemplateCategoryDTO t : tcDAO.getTemplateCategoryListByTemplate(template)) {
			List<QuestionDTO> allQuestions = qDAO.getAllValidQuestionsByCategory(t.getCategory());
			Collections.shuffle(allQuestions);
			for (int i = 0; i < t.getNumberOfQuestions() && i < allQuestions.size() ; i++) {
				questions.add(allQuestions.get(i));
			}
		}
	}
    
}
